package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service("userInfoService")
public class UserInfoServiceImpl implements IUserInfoService {
    @Autowired
    private UserInfoRepository userInfoRepository;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void save(UserInfo userInfo) {
        userInfo.setId(null);
        userInfoRepository.save(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoRepository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        userInfoRepository.deleteById(id);
    }

    @Override
    public UserInfo get(String id) {
        UserInfo userInfo = userInfoRepository.findById(id).get();
        return userInfo;
    }

    @Override
    public List<UserInfo> list() {
        return userInfoRepository.findAll();
    }

    @Override
    public Boolean checkPhone(String phone) {
        UserInfo userInfo = userInfoRepository.findByPhone(phone);
        return userInfo != null;
    }

    @Override
    public void sendVerifyCode(String phone) {
        //设置验证码
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 4);

        StringBuilder sb = new StringBuilder(80);
        sb.append("尊敬的用户你好:").append(" 这是你的验证码").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME)
                .append("秒钟输入你的验证码");
        System.out.println(sb);
        //真实发短信功能

        //restTemplate:Spring封装用于发起http请求工具类
        RestTemplate restTemplate =new RestTemplate();


        userInfoRedisService.setSendVerifyCode(phone, code);
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {
        //判断传入是否为空
        AssertUtil.hasLength(phone, "手机号不能为空");
        AssertUtil.hasLength(nickname, "名称不能为空");
        AssertUtil.hasLength(password, "密码不能为空");
        AssertUtil.hasLength(rpassword, "确认密码不能为空");
        AssertUtil.hasLength(verifyCode, "验证码不能为空");

        //判断2次输入密码是否相等
        AssertUtil.isEquals(password, rpassword, "两次输入密码不一致");
        //手机号码格式必须正确 @TODO  java 手机号正则表达式
        String regex="^1[3|4|5|6|7|8][0-9]{9}$";
        if (!phone.matches(regex)){
            throw new LogicException("手机号格式不正确");
        }
        //手机号码必须进行唯一效验
        if (this.checkPhone(phone)){
            throw new LogicException("该手机以被注册");
        }
        //短信验证必须一致
        String code = userInfoRedisService.getverifyCode(phone);
        if (code!=null && !code.equalsIgnoreCase(verifyCode)){
            throw new LogicException("验证码失效或者错误");
        }
        //注册
        UserInfo userInfo = new UserInfo();

        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password);
        userInfo.setCity("");
        userInfo.setInfo("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setState(UserInfo.STATE_NORMAL);

        this.save(userInfo);
    }

    @Override
    public UserInfo login(String username, String password) {
        AssertUtil.hasLength(username, "账号不能为空");
        AssertUtil.hasLength(password,"密码不能为空" );
        UserInfo user = userInfoRepository.findByPhone(username);

        if (user==null || !user.getPassword().equals(password)){
            throw  new LogicException("账号密码错误");
        }

        return user;
    }

    @Override
    public List<UserInfo> findByCity(String name) {
        List<UserInfo> byCity = userInfoRepository.findByCity(name);
        return byCity;
    }




}
